//1. 安装
//引入 mongoose
const mongoose = require('mongoose');

//2. 连接mongodb数据库  connect 连接
mongoose.connect('mongodb://127.0.0.1/project', { useNewUrlParser: true,useUnifiedTopology: true });

//3. 设置连接的回调
mongoose.connection.on('open', () => {
    //4. 声明文档结构
    const PhoneSchema = new mongoose.Schema({
        brand: String,
        color: String,
        price: Number,
        tags: Array,
    });
    //5. 创建模型对象           集合的名称 创建集合时会变成『复数』
    const PhoneModel = mongoose.model('phone', PhoneSchema);
    //6. 插入数据
    PhoneModel.insertMany([
        {
            brand: '华为',
            color: '路茶灰',
            price: 2399,
            tags: ['电量大','屏幕大','信号好']
        },
        {
            brand: '小米',
            color: '玫瑰红',
            price: 1999,
            tags: ['小巧','性价比高','游戏流畅']
        },
    ], (err, data) => {
        if(err) throw err;
        console.log('插入成功');
        mongoose.connection.close();
    });
});